package cliente.vista;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout.Alignment;

import cliente.controle.ContaControlador;
import cliente.modelo.Conta;

public class DeletaContaView {
	private JFrame tela;
	private JList campoContas;
	private DefaultListModel campoContasConteudo;
	private JScrollPane scrollPane;
	private JPanel painel;
	private JButton deletar;
	private JButton voltar;
	private ActionListener ouvinte;

	public void iniciaDeletarContaView() {
		// TODO Auto-generated method stub
		ouvinte = new acao();
		tela = new JFrame();
		tela.setResizable(false);
		tela.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		tela.setBounds(500, 300, 400, 200);
		tela.setTitle("Deletar Conta");
		painel = new JPanel();

		GroupLayout layout = new GroupLayout(painel);
		painel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		deletar = new JButton("Deletar");
		deletar.addActionListener(ouvinte);
		voltar = new JButton("Voltar");
		voltar.addActionListener(ouvinte);

		organizaFields(layout);

		campoContasConteudo = new DefaultListModel();
		try {
			ContaControlador.atualizaDadosEmCampoContas(campoContasConteudo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		campoContas = new JList(campoContasConteudo);
		scrollPane = new JScrollPane(campoContas);

		tela.setLayout(new BorderLayout());
		tela.add(scrollPane, BorderLayout.CENTER);
		tela.add(painel, BorderLayout.SOUTH);
		tela.setVisible(true);
	}

	private void organizaFields(GroupLayout layout) {
		criaGruposHorizontais(layout);
		criaGruposVerticais(layout);
	}

	private void criaGruposVerticais(GroupLayout layout) {
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(deletar).addComponent(voltar));
		layout.setVerticalGroup(vGroup);
	}

	private void criaGruposHorizontais(GroupLayout layout) {
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup().addComponent(deletar));
		hGroup.addGroup(layout.createParallelGroup().addComponent(voltar));
		layout.setHorizontalGroup(hGroup);
	}

	class acao implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getSource() == deletar) {
				Conta conta = (Conta) campoContas.getSelectedValue();
				try {
					ContaControlador.deleteConta(conta.getId(),
							campoContasConteudo);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (arg0.getSource() == voltar) {
				tela.dispose();
				try {
					new ClienteView().iniciaTela();
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
